package com.wt.struggle.goodbye.user.adapter

import android.content.Context
import android.graphics.BitmapFactory
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.ProBaseAdapter
import com.wt.struggle.goodbye.base.Share
import kotlinx.android.synthetic.main.user_title_item.view.*

class UserTitleAdapter(context: Context, titleList: ArrayList<String>, val picList: ArrayList<Int>, val code: Int) :
    ProBaseAdapter<String>(context, titleList) {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        val vh = holder as VH
        when (code) {
            1 -> {
                vh.checkBox.visibility = View.GONE

                when (position) {
                    0, 5 -> {
                        vh.viewLine.layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            context.resources.getDimension(R.dimen.dp_10).toInt()
                        )
                        vh.imagePoint.visibility = View.GONE
                    }
                    else -> {
                        vh.viewLine.layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            context.resources.getDimension(R.dimen.dp_1).toInt()
                        )
                        vh.imagePoint.visibility = View.GONE
                    }
                }
            }

            2 -> {

                vh.imagePoint.visibility = View.GONE

                when (position) {
                    1 -> {
                        vh.viewLine.layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            context.resources.getDimension(R.dimen.dp_10).toInt()
                        )
                    }

                    2 -> {
                        vh.checkBox.visibility = View.VISIBLE
                        vh.itemRight.visibility = View.GONE

                        val bb = Share.getTui(context)
                        vh.checkBox.isChecked = bb
                    }
                    else -> {
                        vh.viewLine.layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            context.resources.getDimension(R.dimen.dp_1).toInt()
                        )
                    }
                }
            }

        }

        vh.imageViewImg.setImageBitmap(BitmapFactory.decodeResource(context.resources, picList[position]))
        vh.tvTitle.text = list[position]

        vh.checkBox.setOnCheckedChangeListener { compoundButton, b ->
            Share.saveIsTui(context, b)
            changeTui(b)

        }

    }

    /**
     * 改变推送
     */
    private fun changeTui(b: Boolean) {
        if (b) {
            // 打开推送
        } else {
            // 关闭推送
        }

    }

    override fun onCreateView(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = inflater.inflate(R.layout.user_title_item, parent, false)
        return VH(view)

    }

    override fun onUpdateHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any>) {

    }

    inner class VH(view: View) : RecyclerView.ViewHolder(view) {
        val viewLine = view.viewLine
        val imagePoint = view.imagePoint
        val imageViewImg = view.imageViewImg
        val tvTitle = view.tvTitle
        val checkBox = view.checkBox
        val itemRight = view.itemRight

    }
}